package common;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;


import model.Version;

public class APIService {

    private static OkHttpHelper okHttpHelper;

    public APIService() {
        this.okHttpHelper = new OkHttpHelper();
    }

    //    public static String ServerUrl = "http://47.104.29.114:5000/";
//    public static String ServerUrl = "http://192.168.1.104:5000/";
    public static String ServerUrl = "http://10.200.4.123:5000/";


    public static String getAPK() {
        return ServerUrl + "apk/shenhao.apk";
    }

    public static String getTemperature() {
        return ServerUrl + "temperature/getall";
    }

    public static void getVersion(final Handler handler) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = ServerUrl + "apk/ver.txt";
                Version version = new Version();
                try {
                    okHttpHelper = new OkHttpHelper();
                    JSONObject jsonObject = new JSONObject(okHttpHelper.OkGet(url));
                    version.setVersionCode(jsonObject.getInt("versionCode"));
                    version.setVersionName(jsonObject.getString("versionName"));
                    version.setVersionMessage(jsonObject.getString("versionMessage"));
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putString("versionMessage", jsonObject.getString("versionMessage"));
                    bundle.putInt("versionCode", jsonObject.getInt("versionCode"));
                    message.setData(bundle);
                    handler.sendMessage(message);
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
